/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package kivuli.visao.item;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JComponent;
import kivuli.entidade.Figura;

/**
 *
 * @author bpmlab
 */
public abstract class ItemDeDesenho extends JComponent {

    private Figura figura;

    public ItemDeDesenho(Figura figura) {
        this.figura = figura;
        addMouseEvents();
    }

    private void addMouseEvents() {
        this.addMouseListener(new MouseAdapter() {

            @Override
            public void mouseExited(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }
            
            @Override
            public void mouseReleased(MouseEvent e) {
                itemMouseReleased(e);
            }

        });

        this.addMouseMotionListener(new MouseMotionAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                itemMouseDragged(e);
            }

        });
    }

    protected void itemMouseDragged(MouseEvent e) {
        if (this.getParent() != null) {
            try {
                int x = getParent().getMousePosition().x - getWidth() / 2;
                int y = getParent().getMousePosition().y - getHeight() / 2;
                this.figura.setX(x);
                this.figura.setY(y);
                this.setLocation(x, y);
                this.repaint();
            } catch (NullPointerException ex) {
            }
        }
    }

    protected void itemMouseReleased(MouseEvent e) {
    }

    public Figura getFigura() {
        return figura;
    }

    public void setFigura(Figura figura) {
        this.figura = figura;
    }
    
}
